package com.apps.member.service.impl;

import com.apps.goods.model.po.KupaGoods;
import com.apps.goods.service.IKupaGoodsCatService;
import com.apps.goods.service.IKupaGoodsService;
import com.apps.member.model.po.KupaMemberCollectGoods;
import com.apps.member.dao.KupaMemberCollectGoodsMapper;
import com.apps.member.service.IKupaMemberCollectGoodsService;
import com.apps.sexctption.NullException;
import com.apps.sworld.dao.impl.sDaoImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 大鹏
 * @since 2020-10-02
 */
@Service
public class KupaMemberCollectGoodsServiceImpl extends sDaoImpl<KupaMemberCollectGoodsMapper, KupaMemberCollectGoods> implements IKupaMemberCollectGoodsService {

    @Autowired
    private IKupaGoodsService goodsService;

    @Override
    public KupaMemberCollectGoods getOne(Integer member_id, Integer goods_id)
    {
        QueryWrapper<KupaMemberCollectGoods> wrapper=new QueryWrapper<>();
        wrapper.eq("member_id",member_id);
        wrapper.eq("goods_id",goods_id);
        return getOne(wrapper);
    }

    @Override
    public void delete(Integer member_id, Integer goods_id)
    {
        QueryWrapper<KupaMemberCollectGoods> wrapper=new QueryWrapper<>();
        wrapper.eq("member_id",member_id);
        wrapper.eq("goods_id",goods_id);
        delete(wrapper);
    }

    @Override
    public void saveToDB(Integer member_id, Integer goods_id)
    {
        KupaGoods goods=goodsService.getOneById(goods_id);
        if(null == goods)
            throw new NullException("商品"+goods_id+"不存在");
        KupaMemberCollectGoods collectGoods=new KupaMemberCollectGoods();
        collectGoods.setMember_id(member_id);
        collectGoods.setGoods_id(goods_id);
        collectGoods.setGoods_cat_id(goods.getGoods_cat_id());
        saveToDB(collectGoods);
    }
}
